package com.tobby.demo.jms.producer;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.jms.core.SessionCallback;

import com.tibco.tibjms.TibjmsConnectionFactory;

/**
 * Hello world!
 * 
 */
public class JmsProducer {

	private static final int TEXT_MESSAGE = 1;

	private static final int MAP_MESSAGE = 2;

	public static void main(String[] args) throws JMSException {
		JmsProducer jmsProducer = new JmsProducer();
		jmsProducer.sendMessage();
		jmsProducer.sendMessageByJmsTemplate();
	}

	private void sendMessage() throws JMSException {
		// PCS EMS Server URL: tcp://etclnx31u.nam.nsroot.net:8222
		TibjmsConnectionFactory connectionFactory = new TibjmsConnectionFactory("tcp://figarodev01.nam.nsroot.net:2002");
		connectionFactory.setMetric(1);
		connectionFactory.setMulticastEnabled(false);
		// PCS EMS Server Username/Password: PCSUser/PCSUser
		Connection connection = connectionFactory.createConnection("nyremoterouter01", "skynetadmin");
		connection.start();
		Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		// PCS EMS Server Outbound Topic: EM.ETRD.ALL.TO.PCS2
		Destination destination = session.createTopic("citi.credit.ny.test");
		MessageProducer producer = session.createProducer(destination);
		Message message = createMessage(session, TEXT_MESSAGE);
		producer.send(message);
	}

	private void sendMessageByJmsTemplate() {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		final JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("jmsTemplate");
		jmsTemplate.send(new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				return JmsProducer.this.createMessage(session, MAP_MESSAGE);
			}
		});
		jmsTemplate.execute(new SessionCallback<Object>() {
			public Object doInJms(Session session) throws JMSException {
				MessageProducer producer = session.createProducer(jmsTemplate.getDefaultDestination());
				Message message = createMessage(session, JmsProducer.TEXT_MESSAGE);
				producer.send(message);
				return null;
			}
		});
	}

	private Message createMessage(Session session, int messageType) throws JMSException {
		switch (messageType) {
		case TEXT_MESSAGE:
			TextMessage textMessage = session.createTextMessage("The text message created by Tobby");
			return textMessage;
		case MAP_MESSAGE:
			MapMessage mapMessage = session.createMapMessage();
			mapMessage.setString("uuid", "c53e97a7-789d-49ce-9e8c-9d23f28eb4e0");
			mapMessage.setLong("lastUpdateTime", 1456887075049l);
			mapMessage.setStringProperty("PCS_REQUEST_ID", "c53e97a7-789d-49ce-9e8c-9d23f28eb4e0");
			mapMessage.setLongProperty("PCS_REQUEST_ORIG_TIME", 1456887075049l);
			return mapMessage;
		}
		return null;
	}

}
